package com.lxd.config;


import com.lxd.common.constants.RabbitMQConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author Dongdong
 * @注释 rabbitmq 配置类
 */
@Configuration
public class RabbitMQConfig {
    public static final String DELAYED_EXCHANGE = "push_delayed_exchange";

    public static final String DELAYED_QUEUE = "push_delayed_queue";

    private static final String DELAYED_EXCHANGE_TYPE = "x-delayed-message";

    private static final String DELAYED_ROUTING_TYPE_KEY = "x-delayed-type";

    private static final String DELAYED_ROUTING_TYPE_FANOUT = "fanout";
    /**
     *   构建延迟交换机
     */
    @Bean
    public Exchange delayedExchange(){
        Map<String, Object> args = new HashMap<>();
        //指定交换机类型 x-delayed-type 和通讯类型是fanout
        args.put(DELAYED_ROUTING_TYPE_KEY,DELAYED_ROUTING_TYPE_FANOUT);
        return
                new CustomExchange(DELAYED_EXCHANGE, DELAYED_EXCHANGE_TYPE, false, false, args);
    }

    /**
     * 延迟队列
     */
    @Bean
    public Queue delayedQueue(){
        return QueueBuilder.durable(DELAYED_QUEUE).build();
    }

    /**
     * 绑定交换机和队列
     * @param delayedExchange  延迟交换机
     * @param delayedQueue  延迟队列
     * @return
     */
    @Bean
    public Binding delayedBinding(Exchange delayedExchange,Queue delayedQueue){
        //因为是fanout，所以绑定关系是直接绑定。 routingkey 可以不用写
        return BindingBuilder.bind(delayedQueue).to(delayedExchange).with("").noargs();
    }

}
